<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/11/29
 * Time: 14:14
 * 报表
 */
namespace app\api\controller;
use think\Db;
class Report extends Base {

    public function index(){
        $startTime = input('param.startTime');
        $endTime = input('param.endTime');
        $type=input('param.type')?input('param.type'):1;//   1默认  2房屋类型  3仓库
        $typeId = input('param.typeId')?input('param.typeId'):'';
        $where = array();
        if ($type==2){
            $where['a.house_type_id'] = $typeId;
        }elseif ($type==3){
            $where['a.warehouse_id'] = $typeId;
        }else{
            $where = array();
        }

        if($startTime!=''&&$endTime!=''){
            $where['create_time'] = array('between',array($startTime,$endTime));
        }
        $page =input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $res = Db::table('up_in_warehouse')->alias('a')
            ->join('up_house_type uht','uht.house_type_id=a.house_type_id','left')
            ->join('up_spec_type ust','ust.spec_type_id=a.spec_type_id','left')
            ->join('up_warehouse uw','uw.house_id=a.warehouse_id','left')
            ->group('in_name,house_type_id,spec_type_id')
            ->limit($limit * $num, $num)
            ->field('count(a.in_id) as num,a.*,uht.house_type,ust.spec_type,uw.warehouse_name')
            ->where($where)
            ->select();
        foreach ($res as $key=>&$vo){
            $result = Db::table('up_jia')->where('jia_ku_id',$vo['in_id'])->field('jia_name,jia_value')->select();
            $vo['in_img'] = http_type().$vo['in_img'];
            $vo['children'] = $result;
        }
        return json(array('status'=>200,'data'=>$res));
    }

    /****
     * @return \think\response\Json
     * 搜索接口
     */
    public function selectReport(){
        $keyword  = input('param.keyword');
        $page =input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where = array();
        if($keyword!=''){
            $where['in_name'] = array('like',"%$keyword%");
        }
        $res = Db::table('up_in_warehouse')->alias('a')
            ->join('up_house_type uht','uht.house_type_id=a.house_type_id','left')
            ->join('up_spec_type ust','ust.spec_type_id=a.spec_type_id','left')
            ->join('up_warehouse uw','uw.house_id=a.warehouse_id','left')
            ->group('in_name,house_type_id,spec_type_id')
            ->limit($limit * $num, $num)
            ->field('count(a.in_id) as num,a.*,uht.house_type,ust.spec_type,uw.warehouse_name')
            ->where($where)
            ->select();
        foreach ($res as $key=>&$vo){
            $result = Db::table('up_jia')->where('jia_ku_id',$vo['in_id'])->field('jia_name,jia_value')->select();
            $vo['in_img'] = http_type().$vo['in_img'];
            $vo['children'] = $result;
        }
        return json(array('status'=>200,'data'=>$res));
    }
}